package com.hanhai.material.device;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CodegenMybatisFlexCodegen {
    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl("jdbc:mysql://192.168.1.4:3306/scheduling?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setJdbcUrl("jdbc:mysql://192.168.1.4:3306/device_plc?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true");
//        dataSource.setJdbcUrl("jdbc:mysql://47.109.16.23:3306/device_plc?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true");
//        dataSource.setJdbcUrl("jdbc:mysql://47.109.16.23:3306/wages?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setUsername("device_plc");
        dataSource.setPassword("device_plc");
//        dataSource.setUsername("scheduling");
//        dataSource.setPassword("scheduling");

        //创建配置内容，两种风格都可以。
        // 默认配置
        GlobalConfig globalConfig = createGlobalConfigUseStyle1();
        // 指定配置
//        GlobalConfig globalConfig = createGlobalConfigUseStyle2();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    /**
     * 生成原则1
     * 直接生成
     *
     * @return
     */
    public static GlobalConfig createGlobalConfigUseStyle1() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        //设置根包
         globalConfig.setSourceDir("D://瀚海//material//src//main//java");
        globalConfig.setBasePackage("com.hanhai.material.device");
        //设置表前缀和只生成哪些表
        // 前缀，没有不加
//        globalConfig.setTablePrefix("tb_");
        // 填写需要生成的表
//        globalConfig.getStrategyConfig()
//                // 生成所有表
//                .setGenerateSchema("demo");
        globalConfig.setGenerateTable("sys_stock");

        //设置生成 entity
        globalConfig.setEntityGenerateEnable(true);
        // 启用 Lombok
        globalConfig.setEntityWithLombok(true);
        //设置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        // 控制类
        globalConfig.setControllerGenerateEnable(true);
        // 服务类
        globalConfig.setServiceGenerateEnable(true);
        // 服务实现类
        globalConfig.setServiceImplGenerateEnable(true);
        // 文件
        globalConfig.setMapperXmlGenerateEnable(true);


        // 注释配置
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String format = dateFormat.format(date);
        globalConfig.getJavadocConfig()
                .setAuthor("youchuang")
                .setSince(format);
        return globalConfig;
    }

}
